/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-08-11
 * Time: 21:54
 */
public class ThreadLocalDemo {
    private static ThreadLocal<String> localVar = new ThreadLocal<>();

    static void print(String str){
        System.out.println(str + ":" + localVar.get());

        // 释放内存
        localVar.remove();
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalDemo.localVar.set("local_A");
                print("A");
                System.out.println("after remove:" + localVar.get());
            }
        },"A").start();

        Thread.sleep(1000);

        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalDemo.localVar.set("local_B");
                print("B");
                System.out.println("after remove :"+localVar.get());
            }
        },"B").start();
    }
}
